package com.nineclock.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysRoleDTO;
import com.nineclock.system.mapper.SysCompanayUserMapper;
import com.nineclock.system.mapper.SysRoleMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysRole;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class SysCompanyUserServiceImpl implements SysCompanyUserService {

    @Autowired(required = false)
    private SysCompanayUserMapper companayUserMapper;

    @Autowired(required = false)
    private SysRoleMapper roleMapper;

    @Override
    public List<SysCompanyUserDTO> queryCompanyUser(Long userId, Long companyId) {

        // 根据用户id、企业id查询员工列表
        LambdaQueryWrapper<SysCompanyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysCompanyUser::getUserId, userId);
        if (companyId != null) {
            wrapper.eq(SysCompanyUser::getCompanyId, companyId);
        }
        List<SysCompanyUser> sysCompanyUserList = companayUserMapper.selectList(wrapper);

        if(CollectionUtils.isEmpty(sysCompanyUserList)){
            throw new NcException(ResponseEnum.USER_NOT_JOIN_COMPANY);
        }

        // 数据转换，List<SysCompanyUser> --> List<SysCompanyUserDTO>
        List<SysCompanyUserDTO> companyUserDTOList = BeanHelper.copyWithCollection(sysCompanyUserList, SysCompanyUserDTO.class);

        // 封装员工对应的角色、权限信息
        for (SysCompanyUserDTO sysCompanyUserDTO : companyUserDTOList) {
            // 根据员工id查询对应角色、权限信息
            List<SysRole> sysRoleList = roleMapper.queryRoleByCompanyUserId(sysCompanyUserDTO.getId());
            // 数据转换，List<SysRole> --> List<SysRoleDTO>
            List<SysRoleDTO> sysRoleDTOList = BeanHelper.copyWithCollection(sysRoleList, SysRoleDTO.class);
            
            // 设置员工的角色信息
            sysCompanyUserDTO.setRoles(sysRoleDTOList);

            // TODO 同理，需要根据角色id，查询对应的权限信息，并设置
        }
        
        return companyUserDTOList;
    }

    @Override
    public SysCompanyUserDTO queryCompanyUserById(Long companyUserId) {
        SysCompanyUser companyUser = companayUserMapper.selectById(companyUserId);
        return BeanHelper.copyProperties(companyUser,SysCompanyUserDTO.class);
    }

    @Override
    public List<SysCompanyUserDTO> queryAllCompanyUser() {
        LambdaQueryWrapper<SysCompanyUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.eq(SysCompanyUser::getEnable,(short)1);
        List<SysCompanyUser> companyUserList = companayUserMapper.selectList(wrapper);
        return BeanHelper.copyWithCollection(companyUserList,SysCompanyUserDTO.class);
    }
}